package com.cangzhou.cvoa.exception;


import com.cangzhou.cvoa.common.BaseResponse;
import com.cangzhou.cvoa.common.ErrorCode;
import com.cangzhou.cvoa.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:Tian kangNing
 * @Date:Created in 9:24 2022/5/2
 * @Description: 全局异常处理器
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)//这个方法只捕获指定的异常
    public BaseResponse businessExceptionHandler(BusinessException e){

        log.error("BusinessException" + e.getMessage(),e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)//这个方法只捕获指定的异常
    public BaseResponse runtimeExceptionHandler(RuntimeException e){

        log.error("RuntimeException",e);
        return ResultUtils.error(ErrorCode.SYS_ERROR, e.getMessage(),"");
    }
}
